package com.tianhui.consignortwofei.mvp.ui.activity.deliverGoods.normal;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.CommonUtil;
import com.fgs.common.utils.StringUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.TabSwitchView;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/9/1
 * @description
 **/
public class DeliverGoodsStepTwoActivity extends DeliverGoodsActivity {

    @BindView(R.id.activity_deliver_goods_step_two_goodsTypeClickItemView)
    protected ClickItemView mGoodsTypeClickItemView;
    @BindView(R.id.activity_deliver_goods_step_two_goodsNameInputItemView)
    protected InputItemView mGoodsNameInputItemView;
    @BindView(R.id.activity_deliver_goods_step_two_weightInputItemView)
    protected InputItemView mWeightInputItemView;
    @BindView(R.id.activity_deliver_goods_step_three_detachableTabSwitchView)
    protected TabSwitchView mDetachableTabSwitchView;
    @BindView(R.id.activity_deliver_goods_step_three_detachWeightLinearLayout)
    protected LinearLayout mDetachWeightLinearLayout;
    @BindView(R.id.activity_deliver_goods_step_three_detachWeightInputItem)
    protected InputItemView mDetachWeightInputItemView;
    @BindView(R.id.activity_deliver_goods_step_two_quantityInputItemView)
    protected InputItemView mQuantityInputItemView;
    @BindView(R.id.activity_deliver_goods_step_two_priceInputItemView)
    protected InputItemView mPriceInputItemView;
    @BindView(R.id.activity_deliver_goods_step_two_maxPriceInputItemView)
    protected InputItemView mMaxPriceInputItemView;
    @BindView(R.id.activity_deliver_goods_step_two_totalPriceInputItemView)
    protected InputItemView mTotalPriceInputItemView;
    @BindView(R.id.activity_deliver_goods_step_two_priceStatusTabSwitchView)
    protected TabSwitchView mPriceStatusTabSwitchView;
    @BindView(R.id.activity_deliver_goods_step_two_phoneLinearLayout)
    protected LinearLayout mPhoneLinearLayout;
    @BindView(R.id.activity_deliver_goods_step_two_phoneInputItemView)
    protected InputItemView mPhoneInputItemView;

    private SelectDialog mGoodsTypeSelectDialog;
    private String mGoodsWeight;
    private String mPrice;

    @Override
    public int getLayoutId() {
        return R.layout.activity_deliver_goods_step_two;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void initData() {
        mGoodsInfo.ishide = "0";
        mGoodsInfo.detachable = "0";
        //        mGoodsTypeClickItemView.setContent("煤炭及制品");
        //        mGoodsInfo.goodstype = "0100";
        //        mGoodsNameInputItemView.setContent("货物名称");
        //        mWeightInputItemView.setContent("10");
        //        mQuantityInputItemView.setContent("2");
        //        mPriceInputItemView.setContent("111");
    }

    @Override
    public void initView() {
        showStepTwoStatus();
    }

    @Override
    public void initListener() {
        mPriceInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                mPrice = string;
                mMaxPriceInputItemView.setContent(string);
                mTotalPriceInputItemView.setContent(getTotalPrice());
            }
        });
        mWeightInputItemView.setOnInputItemListener(new InputItemView.OnInputItemListener() {
            @Override
            public void onInput(String string) {
                mGoodsWeight = string;
                mTotalPriceInputItemView.setContent(getTotalPrice());
            }
        });
        mPriceStatusTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                mGoodsInfo.ishide = value;
                showBargainPhone(position == 1);
            }
        });

        //是否可拆单
        mDetachableTabSwitchView.setOnTabCheckedChangeListener(new TabSwitchView.OnTabCheckedChangeListener() {
            @Override
            public void onCheckedChanged(int position, String item, String value) {
                if (position == 0) {
                    mDetachWeightLinearLayout.setVisibility(View.VISIBLE);
                    mDetachWeightInputItemView.setContent("0");
                } else {
                    mGoodsInfo.detachweight = "";
                    mDetachWeightLinearLayout.setVisibility(View.GONE);
                }
                mGoodsInfo.detachable = value;
            }
        });
    }

    //是否显示议价电话View
    private void showBargainPhone(boolean isShow) {
        if (isShow) {
            mPhoneLinearLayout.setVisibility(View.VISIBLE);
        } else {
            mPhoneLinearLayout.setVisibility(View.GONE);
        }
    }

    private String getTotalPrice() {

        //数据失精处理

        mPrice = TextUtils.isEmpty(mPrice) ? "0" : mPrice;
        mGoodsWeight = TextUtils.isEmpty(mGoodsWeight) ? "0" : mGoodsWeight;

        String totalPrice = new BigDecimal(mPrice).multiply(new BigDecimal(mGoodsWeight)).toString();//两个数字相乘（加减乘除的单词就是方法）

        return CommonUtil.keepTwoDecimal(totalPrice);
    }

    private void requestGoodsType() {
        Map<String, String> map = new HashMap<>();
        map.put("pid", "73");
        mOrderModel.dictionaryModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DictionaryInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DictionaryInfo>> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        List<DictionaryInfo> dictionaryInfoList = commonResponse.getData();
                        if (dictionaryInfoList != null && dictionaryInfoList.size() > 0) {
                            showGoodsTypeDialog(dictionaryInfoList);
                        }
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void showGoodsTypeDialog(List<DictionaryInfo> dictionaryInfoList) {
        mGoodsTypeSelectDialog = new SelectDialog<DictionaryInfo>(this);
        mGoodsTypeSelectDialog.setTitle("请选择货物类型");
        mGoodsTypeSelectDialog.setList(dictionaryInfoList);
        mGoodsTypeSelectDialog.show();

        mGoodsTypeSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<DictionaryInfo>() {
            @Override
            public void onItemClick(DictionaryInfo iSelectViewItem, int position) {
                mGoodsTypeClickItemView.setContent(iSelectViewItem.getSelectItemText());
                mGoodsInfo.goodstype = iSelectViewItem.getSelectItemTextValue();
            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_deliver_goods_step_two_goodsTypeClickItemView)
    public void goodsType() {
        if (mGoodsTypeSelectDialog == null) {
            requestGoodsType();
        } else {
            mGoodsTypeSelectDialog.show();
        }

    }

    @OnClick(R.id.activity_deliver_goods_step_two_nextButton)
    public void next() {
        String goodsName = mGoodsNameInputItemView.getContent();
        String weight = mWeightInputItemView.getContent();
        String quantity = mQuantityInputItemView.getContent();
        String price = mPriceInputItemView.getContent();
        String maxPrice = mMaxPriceInputItemView.getContent();
        String totalPrice = mTotalPriceInputItemView.getContent();
        String phone = mPhoneInputItemView.getContent();
        mGoodsInfo.goodsdescription = goodsName;
        mGoodsInfo.weight = weight;
        mGoodsInfo.quantity = quantity;
        mGoodsInfo.baseprice = price;
        mGoodsInfo.bigprice = maxPrice;
        mGoodsInfo.totalprice = totalPrice;

        String detachWeight = mDetachWeightInputItemView.getContent();

        if (TextUtils.isEmpty(mGoodsInfo.goodstype)) {
            showToast("请选择货物类型");
            return;
        }
//        if (TextUtils.isEmpty(mGoodsInfo.goodsdescription)) {
//            showToast("请填写货物名称");
//            return;
//        }
        if (TextUtils.isEmpty(mGoodsInfo.weight)) {
            showToast("请填写货物重量");
            return;
        }
        if (!TextUtils.isEmpty(mGoodsInfo.detachable) && mGoodsInfo.detachable.equals("1")) {
            if (TextUtils.isEmpty(detachWeight)) {
                showToast("请填写拆单重量");
                return;
            } else {

                if (Double.parseDouble(detachWeight) == 0) {
                    showToast("拆单重量不能为0");
                    return;
                }
                if (StringUtil.compareTwoString(detachWeight, mGoodsInfo.weight) == 1) {
                    showToast("拆单重量不能大于发单重量");
                    return;
                }
                if (StringUtil.compareTwoString(detachWeight, mGoodsInfo.weight) == 0) {
                    showToast("拆单重量不能等于发单重量");
                    return;
                }
                mGoodsInfo.detachweight = detachWeight;
            }

        }

        if (TextUtils.isEmpty(mGoodsInfo.quantity)) {
//            showToast("请填写货物数量");
//            return;
            mGoodsInfo.quantity = "0";
        }
        if (TextUtils.isEmpty(mGoodsInfo.baseprice)) {
            showToast("请填写运费单价");
            return;
        }
        if (TextUtils.isEmpty(mGoodsInfo.bigprice)) {
            showToast("请填写最高接受价");
            return;
        }
        if (StringUtil.compareTwoString(mGoodsInfo.baseprice, mGoodsInfo.bigprice) == 1) {
            showToast("最高接受价不能小于运费单价");
            return;
        }

        if (!TextUtils.isEmpty(mGoodsInfo.ishide) && mGoodsInfo.ishide.equals("1")) {
            if (TextUtils.isEmpty(phone)) {
                showToast("请填写议价电话");
                return;
            }
            mGoodsInfo.facetel = phone;
        } else {
            mGoodsInfo.facetel = "";
        }

        openNextActivity();

    }

    protected void openNextActivity() {
        openNextStepActivity(DeliverGoodsStepThreeActivity.class);
    }

}
